package com.sline.servlet.demo1_servlet._01_base;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class _06_Register extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int pageState = 0;
        if ((username != null && !"".equals(username.trim())) && (password != null && !"".equals(password.trim()))) {
            if ("sline".equals(username.trim()) && "abc123".equals(password.trim())) {
                pageState = 1;
            } else {
                pageState = 2;
            }
        }
        PrintWriter writer = resp.getWriter();
        if (pageState == 1) {   // 登陆成功
            writer.println("<body>");
            writer.println("<div style='color: red'>恭喜,登陆成功</div>");
            writer.println("</body>");
        } else {                // 登陆错误 / 未登录
            writer.println("<body>");
            writer.print("<form action='#' method='post'>");
            writer.print("<fieldset>");
            if (pageState == 2) {
                writer.println("<div style='color: red'>用户名或密码错误</div>");
            }
            writer.print("<br/>");
            writer.print("<input name='username' required='required' placeholder='用户名'/>");
            writer.print("<br/>");
            writer.print("<br/>");
            writer.print("<input type='password' name='password' required='required' placeholder='密码'/>");
            writer.print("<br/>");
            writer.print("<br/>");
            writer.print("<input type='submit' value='登陆'/>");
            writer.print("</fieldset>");
            writer.print("</form>");
            writer.println("</body>");
        }

    }
}
